μ€νλ§ ν΅μ¬ μ리 - κΈ°λ³ΈνΈμ λ€μΌλ©° μ 리νλ POSTμ λλ€.
π― μ»΄ν¬λνΈ μ€μΊκ³Ό μμ‘΄κ΄κ³ μλ μ£Όμ μμνκΈ°
μ§κΈκΉμ§λ μ€νλ§ λΉ λ±λ‘ μ, μλ° μ½λμ @Bean μ΄λ XMLμ <bean></bean> μ ν΅ν΄ μ€μ μ 보μ μ§μ λ±λ‘ν μ€νλ§ λΉμ λͺ
μνλ€.
- νμ§λ§ λ±λ‘ν΄μΌ ν μ€νλ§ λΉμ μκ° λ§μμ§κ² λλ©΄, μ΄λ¬ν κ³Όμ μ λ§€μ° λΉν¨μ¨μ μ΄λ€.
μ€νλ§μ μ€μ μ λ³΄κ° μμ΄λ μλμΌλ‘ μ€νλ§ λΉμ λ±λ‘νλ μ»΄ν¬λνΈ μ€μΊμ΄λΌλ κΈ°λ₯μ μ 곡νλ€!
- λν μμ‘΄κ΄κ³λ μλμΌλ‘ μ£Όμ
νλ
@AutowiredλΌλ κΈ°λ₯λ μ 곡νλ€.
λ¨Όμ μ½λλ‘ μμ보기 μν΄, μλ‘μ΄ AutoAppConfig.java λ₯Ό μμ±νλ€.
package hello.core;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {
}
-
@ComponentScanμ΄ μΆκ°λμλ€. μ΄λ μ€νλ§ λΉμ λ€ μ½μ΄ μλμΌλ‘ λ±λ‘νκΈ° μν΄ νμν μ΄λ Έν μ΄μ μ΄λ€.- μ΄λ
@Componentμ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€λ₯Ό μ°Ύμ μλμΌλ‘ μ€νλ§ λΉμΌλ‘ λ±λ‘ν΄μ€λ€. excludeFiltersλ μ΄λ μ μΈν ν΄λμ€λ₯Ό μλ―Ένλ€.- νμ¬ μμ μμλ μ΄μ μ μμ±νλ
AppConfigμ@Configurationμ΄λ Έν μ΄μ μ΄ λΆμ΄μκ³ , μ΄λ₯Ό μμ νμ§ μκΈ°μ μ΄λ₯Ό μ μΈνλ€.
- μ΄λ
- κΈ°μ‘΄μ
AppConfigμ λ€λ₯΄κ²@BeanμΌλ‘ λ±λ‘ν ν΄λμ€κ° νλλ μλ€!
μ΄μ κ° ν΄λμ€κ° μ»΄ν¬λνΈ μ€μΊμ λμμ΄ λλλ‘ @Component μ΄λ
Έν
μ΄μ
μ λΆμ¬μ€λ€.
MemoryMemberRepository @Component μΆκ°, RateDiscountPolicy @Component μΆκ°
MemberServiceImpl @Component, @Autowired μΆκ°
package hello.core.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
@Override
public void join(Member member) {
memberRepository.save(member);
}
@Override
public Member findMember(Long memberId) {
return memberRepository.findById(memberId);
}
}
- μ΄μ
AppConfigμμλ@BeanμΌλ‘ μ§μ μ€μ μ 보λ₯Ό μμ±νκ³ , μμ‘΄κ΄κ³λ μ§μ λͺ μνλ€. -
νμ§λ§ μ΄μ λ μ»΄ν¬λνΈ μ€μΊμΌλ‘ μλ λ±λ‘νκΈ°μ μμ‘΄κ΄κ³ μ£Όμ λν ν΄λΉ ν΄λμ€ λ΄μμ μ²λ¦¬ν΄μΌ νλ€.
- μ΄λ μ¬μ©νλ κ²μ΄
@Autowiredμ΄λ Έν μ΄μ μ΄λ€. μ΄λ μμ‘΄κ΄κ³λ₯Ό μλμΌλ‘ μ£Όμ ν΄μ€λ€.
- μ΄λ μ¬μ©νλ κ²μ΄
OrderServiceImpl @Component, @Autowired μΆκ°
package hello.core.order;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.Member;
import hello.core.member.MemberRepository;
import hello.core.member.MemoryMemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class OrderServiceImpl implements OrderService {
// νμμ μ°ΎκΈ° μν΄ νμ
private final MemberRepository memberRepository;
// ν μΈ μ μ±
μ¬μ©μ μν΄ νμ
// private final DiscountPolicy discountPolicy = new FixDiscountPolicy();
private final DiscountPolicy discountPolicy;
@Autowired
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}
@Override
public Order createOrder(Long memberId, String itemName, int itemPrice) {
Member member = memberRepository.findById(memberId);
// ν μΈμ λν΄μλ createOrderλ μμ μμ§ λͺ»ν¨, λ¨μΌ μ±
μ μμΉμ μ μ§ν¨ μ
int discountPrice = discountPolicy.discount(member, itemPrice);
return new Order(memberId, itemName, itemPrice, discountPrice);
}
}
@Autowiredλ₯Ό μ¬μ©νλ©΄ μμ±μμμ μ¬λ¬ μμ‘΄κ΄κ³λ ν λ²μ μ£Όμ λ°μ μ μλ€.
μ΄μ μμ±ν μ€μ μ λ³΄λ‘ μ€νλ§ λΉ λ±λ‘μ΄ μ μ μνλλμ§ νμΈνλ ν μ€νΈ μ½λλ₯Ό μμ±νλ€.
AutoAppConfigTest.java
package hello.core.scan;
import hello.core.AutoAppConfig;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.*;
public class AutoAppConfigTest {
@Test
void basicScan() {
ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
assertThat(memberService).isInstanceOf(MemberService.class);
}
}
- μ€μ μ λ³΄λ‘ λ°©κΈ μμ±ν
AutoAppConfigλ₯Ό λ겨μ€λ€.
πͺ μ»΄ν¬λνΈ μ€μΊκ³Ό μλ μμ‘΄κ΄κ³ μ£Όμ λμ κ³Όμ
@ComponentScan
@ComponentScanμ@Componentκ° λΆμ λͺ¨λ ν΄λμ€λ₯Ό μ€νλ§ λΉμΌλ‘ λ±λ‘νλ€. (μ€νλ§ μ»¨ν μ΄λκ° λͺ¨λ ν΄λμ€λ₯Ό νμΈ!, μ±κΈν€μΌλ‘ λ±λ‘)-
μ΄λ μ€νλ§ λΉμ κΈ°λ³Έ μ΄λ¦μ ν΄λμ€λͺ μ μ¬μ©νλ, 맨 μκΈμλ§ μλ¬Έμλ₯Ό μ¬μ©νλ€.
- λ§μ½ λΉ μ΄λ¦μ μ§μ νκ³ μΆμΌλ©΄
@Component("memberService2")μ κ°μ΄ λΆμ¬νλ©΄ λλ€.
- λ§μ½ λΉ μ΄λ¦μ μ§μ νκ³ μΆμΌλ©΄
@Autowiredμμ‘΄κ΄κ³ μλ μ£Όμ
- μμ±μμ
@Autowiredλ₯Ό μ§μ νλ©΄, μ€νλ§ μ»¨ν μ΄λκ° μλμΌλ‘ ν΄λΉ μ€νλ§ λΉμ μ°Ύμμ μ£Όμ νλ€. -
μ΄λ κΈ°λ³Έ μ‘°ν μ λ΅μ νμ μ΄ κ°μ λΉμ μ°Ύμ μ£Όμ νλ€.
getBean(MemberRepository.class)μ λμΌνλ€κ³ μ΄ν΄νλ©΄ μ½λ€.- νμ μ΄ κ°μ λΉμ΄ μ¬λ¬ κ°μΈ κ²½μ°λ λ€μμ ...
- μμ±μμ νλΌλ―Έν°κ° λ§μλ, λ€ μ°Ύμμ μλμΌλ‘ μ£Όμ νλ€.
π― νμ μμΉμ κΈ°λ³Έ μ€μΊ λμ
πͺ νμν ν¨ν€μ§μ μμ μμΉ μ§μ
μ»΄ν¬λνΈ μ€μΊ μ, λͺ¨λ μλ° ν΄λμ€λ₯Ό νμΈνλ©΄ μκ°μ΄ μ€λ κ±Έλ¦°λ€. κ·Έλμ νμν μμΉλΆν° νμνλλ‘ μμ μμΉλ₯Ό μ§μ ν μ μλ€.
package hello.core;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class),
basePackages = "hello.core"
)
public class AutoAppConfig {
}
basePackages: νμν ν¨ν€μ§μ μμ μμΉλ₯Ό μ§μ νλ€. μ΄ ν¨ν€μ§λ₯Ό ν¬ν¨ν νμ ν¨ν€μ§λ₯Ό λͺ¨λ νμνλ€. μ¬λ¬ μμ μμΉλ μ§μ κ°λ₯νλ€.basePackageClasses: μ§μ ν ν΄λμ€μ ν¨ν€μ§λ₯Ό νμ μμ μμΉλ‘ μ§μ νλ€.- μ§μ νμ§ μλλ€λ©΄,
@ComponentScanμ΄ λΆμ μ€μ μ 보 ν΄λμ€μ ν¨ν€μ§κ° μμ μμΉκ° λλ€.
κΆμ₯νλ λ°©λ²
- ν¨ν€μ§ μμΉλ₯Ό μ§μ νμ§ μκ³ , μ€μ μ 보 ν΄λμ€μ μμΉλ₯Ό νλ‘μ νΈ μ΅μλ¨μ λλ κ²μ΄λ€.
-
μ΅κ·Ό μ€νλ§ λΆνΈλ μ΄ λ°©λ²μ κΈ°λ³ΈμΌλ‘ μ 곡νλ€.
- μ€νλ§ λΆνΈλ₯Ό μ¬μ©νλ©΄ μ€νλ§ λΆνΈμ λν μμ μ 보μΈ
@SpringBootApplicationμ΄λ Έν μ΄μ μ νλ‘μ νΈ μμ λ£¨νΈ μμΉμ λλ κ²μ΄ κ΄λ‘μ΄κ³ , μ΄ μ€μ μμ@ComponentScanμ΄λ Έν μ΄μ μ΄ λ€μ΄μλ€.
- μ€νλ§ λΆνΈλ₯Ό μ¬μ©νλ©΄ μ€νλ§ λΆνΈμ λν μμ μ 보μΈ
πͺ μ»΄ν¬λνΈ μ€μΊ κΈ°λ³Έ λμ
μ»΄ν¬λνΈ μ€μΊμ λ€μκ³Ό κ°μ μ΄λ Έν μ΄μ λ μΆκ°λ‘ λμμ ν¬ν¨νλ€.
@Component: μ»΄ν¬λνΈ μ€μΊμμ μ¬μ©@Controller: μ€νλ§ MVC 컨νΈλ‘€λ¬μμ μ¬μ©@Service: μ€νλ§ λΉμ¦λμ€ λ‘μ§μμ μ¬μ©@Repository: μ€νλ§ λ°μ΄ν° μ κ·Ό κ³μΈ΅μμ μ¬μ©@Configuration: μ€νλ§ μ€μ μ 보μμ μ¬μ©
μ ν΄λμ€μ μμ€ μ½λλ₯Ό 보면 λͺ¨λ
@Componentλ₯Ό ν¬ν¨νκ³ μλ κ²μ νμΈν μ μλ€.
μ¬μ€ μ΄λ Έν μ΄μ μ μμ κ΄κ³λΌλ κ²μ΄ μλ€. κ·Έλμ μ΄λ Έν μ΄μ μ΄ νΉμ μ΄λ Έν μ΄μ μ λ€κ³ μλ κ²μ μΈμνλ κ²μ μλ° μ 곡 κΈ°λ₯μ΄ μλ, μ€νλ§ μ 곡 κΈ°λ₯μ΄λ€.
μ»΄ν¬λνΈ μ€μΊμ μ©λ λΏ μλλΌ λ€μ μ΄λ Έν μ΄μ μ΄ μλ€λ©΄ μ€νλ§μ λΆκ° κΈ°λ₯μ μννλ€.
@Controller: μ€νλ§ MVC 컨νΈλ‘€λ¬λ‘ μΈμ-
@Repository: μ€νλ§ λ°μ΄ν° μ κ·Ό κ³μΈ΅μΌλ‘ μΈμνκ³ , λ°μ΄ν° κ³μΈ΅μ μμΈλ₯Ό μ€νλ§ μμΈλ‘ λ³νν΄μ€λ€.- DBκ° λ³κ²½λμμ λ, μμΈλ λ³κ²½λμ΄ λ€λ₯Έ κ³μΈ΅μ μ½λ λν λ³κ²½λμ΄μΌ νλ λ¬Έμ λ₯Ό λ°©μ§νλ€.
@Configuration: μ€νλ§ μ€μ μ λ³΄λ‘ μΈμνκ³ , μ€νλ§ λΉμ΄ μ±κΈν€μ μ μ§νλλ‘ μΆκ° μ²λ¦¬λ₯Ό νλ€.@Service: νΉλ³ν μ²λ¦¬λ₯Ό νμ§ μλλ€. λΉμ¦λμ€ κ³μΈ΅μ κ°λ°μλ€μ΄ μΈμνλλ° λμμ μ€λ€.
useDefaultFiltersμ΅μ μ κΈ°λ³ΈμΌλ‘ μΌμ Έμλ€. μ΄ μ΅μ μ λλ©΄ κΈ°λ³Έ μ€μΊ λμλ€μ΄ μ μΈλλ€.
π― νν°
includeFilters: μ»΄ν¬λνΈ μ€μΊ λμμ μΆκ°λ‘ μ§μ νλ€.excludeFilters: μ»΄ν¬λνΈ μ€μΊμμ μ μΈν λμμ μ§μ νλ€.
μ»΄ν¬λνΈ μ€μΊ λμμ μΆκ°ν μ΄λ Έν μ΄μ
package hello.core.scan.filter;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent {
}
//
package hello.core.scan.filter;
@MyIncludeComponent
public class BeanA {
}
TYPE: ν΄λμ€ λ 벨μ λΆλ μ΄λ Έν μ΄μ MyIncludeComponentμ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€(μμμλBeanA)λ μ»΄ν¬λνΈ μ€μΊμ μΆκ°ν κ²μ΄λ€!!!
μ»΄ν¬λνΈ μ€μΊ λμμμ μ μΈν μ΄λ Έν μ΄μ
package hello.core.scan.filter;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}
//
package hello.core.scan.filter;
@MyExcludeComponent
public class BeanB {
}
MyExcludeComponentμ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€(μμμλBeanB)λ μ»΄ν¬λνΈ μ€μΊμμ μ μΈν κ²μ΄λ€!!!
μ€μ μ 보μ ν μ€νΈ μ½λ
package hello.core.scan.filter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.context.annotation.ComponentScan.*;
public class ComponentFilterAppConfigTest {
@Test
void filterScan() {
ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
BeanA beanA = ac.getBean("beanA", BeanA.class);
assertThat(beanA).isNotNull();
Assertions.assertThrows(
NoSuchBeanDefinitionException.class,
() -> ac.getBean("beanB", BeanB.class)
);
}
@Configuration
@ComponentScan(
includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
)
static class ComponentFilterAppConfig {
}
}
ComponentFilterAppConfigμ€μ μ 보λ μ»΄ν¬λνΈ μ€μΊμ μννλλ°,MyIncludeComponentμ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€λ μ»΄ν¬λνΈ μ€μΊ λμμ ν¬ν¨νκ³ ,MyExcludeComponentμ΄λ Έν μ΄μ μ΄ λΆμ ν΄λμ€λ ν¬ν¨νμ§ μλλ€.
πͺ FilterType μ΅μ
5κ°μ§ μ΅μ μ΄ μλ€.
-
ANNOTATION: κΈ°λ³Έκ°, μ΄λ Έν μ΄μ μ μΈμν΄μ λμνλ€. (μλ΅ κ°λ₯)- ex.
org.example.SomeAnnotation
- ex.
-
ASSIGNABLE_TYPE: μ§μ ν νμ κ³Ό μμ νμ μ μΈμν΄μ λμνλ€.- ex.
org.example.SomeClass
- ex.
-
ASPECTJ: AspectJ ν¨ν΄ μ¬μ©- ex.
org.example..*Service+
- ex.
-
REGEX: μ κ· ννμ- ex.
org\.example\.Default.*
- ex.
-
CUSTOM:TypeFilterλΌλ μΈν°νμ΄μ€λ₯Ό ꡬνν΄μ μ²λ¦¬- ex.
org.example.MyTypeFilter
- ex.
μλ₯Ό λ€μ΄ BeanA ν΄λμ€λ μ»΄ν¬λνΈ μ€μΊμμ μ μΈνκ³ μΆλ€λ©΄,
@Configuration
@ComponentScan(
includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
excludeFilters = {
@Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
@Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BeanA.class)
}
)
includeFiltersλ₯Ό κ±°μ μ¬μ©ν μΌμ μλ€!@Componentλ©΄ μΆ©λΆνλ€...
π― μ€λ³΅ λ±λ‘κ³Ό μΆ©λ
μ»΄ν¬λνΈ μ€μΊμμ κ°μ λΉ μ΄λ¦μ λ±λ‘νλ©΄ μ΄λ»κ² λ κΉ? λ€μκ³Ό κ°μ λ κ°μ§ μν©μ΄ μλ€.
- μλ λΉ λ±λ‘ vs. μλ λΉ λ±λ‘
- μλ λΉ λ±λ‘ vs. μλ λΉ λ±λ‘
πͺ μλ λΉ λ±λ‘ vs. μλ λΉ λ±λ‘
μ»΄ν¬λνΈ μ€μΊμ μν΄ μλμΌλ‘ μ€νλ§ λΉμ΄ λ±λ‘λλλ°, κ·Έ μ΄λ¦μ΄ κ°μ κ²½μ° μ€νλ§μ μ€λ₯λ₯Ό λ°μμν¨λ€.
ConflictingBeanDefinitionExceptionμμΈ λ°μ!
πͺ μλ λΉ λ±λ‘ vs. μλ λΉ λ±λ‘
@Component
public class MemoryMemberRepository implements MemberRepository {}
- μλ λΉ λ±λ‘
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class),
basePackages = "hello.core"
)
public class AutoAppConfig {
// λΉ λ±λ‘ μ, 맨 μ κΈμλ μλ¬Έμλ‘!
@Bean(name = "memoryMemberRepository")
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
}
- μλ λΉ λ±λ‘
- κ·Έλ¦¬κ³ μ΄λ κ² μλ λΉ λ±λ‘κ³Ό μλ λΉ λ±λ‘μ΄ μΆ©λλλ κ²½μ°, μλ λΉ λ±λ‘μ΄ μ°μ κΆμ κ°μ Έ μλ λΉμ΄ μλ λΉμ μ€λ²λΌμ΄λ©ν΄λ²λ¦°λ€.
μλ λΉ λ±λ‘ μ λ¨λ λ‘κ·Έ
Overriding bean definition for bean 'memoryMemberRepository' with a different definition: replacing
- μ΅κ·Ό μ€νλ§ λΆνΈμμλ μλ λΉ λ±λ‘κ³Ό μλ λΉ λ±λ‘μ΄ μΆ©λλλ©΄, μ€λ₯κ° λ°μνλλ‘ κΈ°λ³Έ κ°μ λ°κΏ¨λ€. (μ€νλ§ λΆνΈμΈ
CoreApplicationμ μ€νν΄λ³΄λ©΄ μ€λ₯λ₯Ό νμΈν μ μλ€.)
νμ μ‘κΈ° μ΄λ €μ΄ λ²κ·Έλ μ λ§€ν λ²κ·Έλ€!
π μ€μν κ°λ
μ»΄ν¬λνΈ μ€μΊ, μ΄λ Έν μ΄μ